﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Cors;
using XQ.Framework.io.text;
using XQ.WeChatOfficialAccount.BLL.Sys;
using XQ.WeChatOfficialAccount.DAL;
using XQ.WeChatOfficialAccount.DAL.Model;
using XQ.WeChatOfficialAccount.WebAPI.Filter;
using XQ.WeChatOfficialAccount.WebAPI.Models;

namespace XQ.WeChatOfficialAccount.WebAPI.Controllers.System
{
    /// <summary>
    /// 部门信息
    /// </summary>
    //[FormAuthenticationFilter]
    public class DepartmentController : ApiController
    {
        DepartmentBll departmentBll = new DepartmentBll();

        /// <summary>
        /// 获取部列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/Department/GetDepartmenstList")]
        public MenuModel GetDepartmenstList(string deptName="")
        {
            var dd = new DepartmentBll();
            MenuModel menus = dd.GetDepartmentList(deptName);

            return menus;
        }

        /// <summary>
        /// 保存部门
        /// </summary>
        /// <param name="dep"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/Department/SaveDepartment")]
        public string SaveDepartment(string dep)
        {
            var departments = JsonHelper.ParseJson<Sys_Departments>(dep);
            var dd = new DepartmentBll();
            string res = dd.SaveDepartment(departments);

            return res;
        }
        /// <summary>
        /// 删除部门
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/Department/DeleteDepartment")]
        public string DeleteDepartment(int id)
        {
            var dd = new DepartmentBll();
            return dd.DeleteDepartment(id);
        }

        /// <summary>
        /// 删除部门(多删)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Api/Department/DeleteDepartmentByIds")]
        public bool DeleteDepartmentByIds(List<int> ids)
        {
            var dd = new DepartmentBll();
            return dd.DeleteDepartmentByIds(ids);
        }

        /// <summary>
        /// 根据经纬度获取部门
        /// </summary>
        /// <param name="lng"></param>
        /// <param name="lat"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/Department/GetDeptByLnglat")]
        public string GetDeptByLnglat(float lng, float lat)
        {
            string reStr = "{\"name\":\"\", \"code\":\"\"}";
            string deptName = "";
            int sqDeptId = 0;
            int pqDeptId = 0;
            Point curPoint = new Point((int)(lng * 1000000), (int)(lat * 1000000));

            List<Sys_Boundary> depts = new DepartmentBll().GetHasCodeDept();
            foreach (Sys_Boundary d in depts)
            {
                if (CheckIsIn(curPoint, d.Latlngs))
                {
                    //社区级部门
                    sqDeptId = (int)d.deptpartmentID;
                    break;
                }
            }
            //查找片区是否包含此点
            if (sqDeptId > 0)
            {
                deptName = new DepartmentBll().GetDeptNameById(sqDeptId);
                string deptCode = new DepartmentBll().GetDeptCodeById(sqDeptId);
                reStr = "{\"name\":\"" + deptName + "\", \"code\":\"" + deptCode + "\"}";
                List<Sys_Boundary> list = new DepartmentBll().GetDeptByParentId(sqDeptId);
                foreach (Sys_Boundary d in list)
                {
                    if (CheckIsIn(curPoint, d.Latlngs))
                    {
                        //片区级部门
                        pqDeptId = (int)d.deptpartmentID;
                        break;
                    }
                }
                if (pqDeptId > 0)
                {
                    deptName += "/" + new DepartmentBll().GetDeptNameById(pqDeptId);
                    deptCode = new DepartmentBll().GetDeptCodeById(pqDeptId);
                    reStr = "{\"name\":\"" + deptName + "\", \"code\":\"" + deptCode + "\"}";
                }

            }

            return reStr;

        }


        /// <summary>
        /// 检测点在不在多边形之内
        /// </summary>
        /// <param name="curPoint"></param>
        /// <param name="latlngs"></param>
        /// <returns></returns>
        public bool CheckIsIn(Point curPoint, string latlngs)
        {
            GraphicsPath gPath = new GraphicsPath();
            Region region = new Region();
            gPath.Reset();

            string[] pointArr = latlngs.Split(',');
            List<Point> list = new List<Point>();
            for (int i = 0; i < pointArr.Length; i = i + 2)
            {
                list.Add(new Point((int)(Convert.ToDouble(pointArr[i]) * 1000000), (int)(Convert.ToDouble(pointArr[i + 1]) * 1000000)));
            }
            gPath.AddPolygon(list.ToArray());
            region.MakeEmpty();
            region.Union(gPath);

            //返回判断点是否在多边形里面
            return region.IsVisible(curPoint);
        }

        /// <summary>
        /// 根据部门ID查找中心点
        /// </summary>
        /// <param name="deptId"></param>
        /// <returns></returns>
        [Route("Api/Department/GetCenterByDeptID")]
        public string GetCenterByDeptID(int deptId)
        {
            return new DepartmentBll().GetCenterByDeptID(deptId);
        }

        /// <summary>
        /// 获取2级部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/Department/GetDepartmentsByDepth")]
        public IEnumerable<Sys_Departments> GetDepartmentsByDepth()
        {
            var dd = new DepartmentBll();
            List<Sys_Departments> data = dd.GetDepartmentsByDepth(2);
            return data;
        }

        /// <summary>
        /// 获取2级部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/Department/GetDepartmentsByCun")]
        public IEnumerable<Sys_Departments> GetDepartmentsByCun()
        {
            return new DepartmentBll().GetDepartmentsByCun();
        }

        /// <summary>
        /// 根据单位类型获取部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Api/Department/GetDepartmentByType")]
        public IEnumerable<Sys_Departments> GetDepartmentByType(int? Id = 0, string deptName="")
        {
            DepartmentBll bll = new DepartmentBll();
            List<Sys_Departments> dictModels = bll.GetDepartmentByType(Id, deptName);
            dictModels.Insert(0, new Sys_Departments
            {
                Id = -1,
                DepartmentName = "请选择"
            });
            return dictModels;
        }

        /// <summary>
        /// 保存部门
        /// </summary>
        /// <param name="department">部门对象</param>
        /// <returns></returns>
        [HttpPost]
        [Route("Api/Department/Save")]
        public string SaveDepartment(Sys_Departments department)
        {
            string res = departmentBll.SaveDepartment(department);
            return res;
        }

        /// <summary>
        /// 根据ID获取部门详情
        /// </summary>
        /// <param name="depId">部门ID</param>
        /// <returns></returns>
        [Route("Api/Department/GetDepartmentById")]
        public Sys_Departments GetDepartmentById(int depId)
        {
            return departmentBll.GetDepById(depId);
        }

        /// <summary>
        /// 获取指定部门下一级的所有子部门
        /// </summary>
        /// <param name="depId">部门ID</param>
        /// <returns></returns>
        [Route("Api/Department/GetAllChildDeptByParentID")]
        public IEnumerable<Sys_Departments> GetAllChildDeptByParentID(int depId)
        {
            return departmentBll.GetAllChildDeptByParentID(depId);
        }

        /// <summary>
        /// 获取指定部门下一级的所有子部门
        /// </summary>
        /// <param name="depId">部门ID</param>
        /// <returns></returns>
        [Route("Api/Department/GetAllChildDeptsByParentID")]
        public ResultModel GetAllChildDeptsByParentID(int depId)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                resultModel.success = true;
                resultModel.code = "10000";
                resultModel.message = "操作成功";
                resultModel.data = departmentBll.GetAllChildDeptByParentID(depId); ;
                return resultModel;
            }
            catch (Exception ex)
            {
                resultModel.success = false;
                resultModel.code = "10001";
                resultModel.message = ex.Message;
                return resultModel;
            }
        }
    }
}